# @Time    :2025/4/1 8:34
"""
题目3：多态特性
定义一个Shape类，有一个抽象方法area(方法体为空)。再定义Rectangle类和Circle类继承自Shape
类，分别实现area方法计算矩形面积(长×宽)和圆的面积(pir^2)。创建Rectangle和Circle类的对象，
将它们放入一个列表中，遍历列表并调用每个对象的area方法。
"""

import math


class Shape:
    def area(self):
        pass


class Rectangle(Shape):  # 矩形
    def __init__(self, width, height):
        self.width = width
        self.height = height

    def area(self):
        return self.width * self.height


class Circle(Shape):  # 圆
    def __init__(self, radius):
        self.radius = radius

    def area(self):
        return math.pi * self.radius ** 2


r1 = Rectangle(10, 20)
c1 = Circle(10)

# 将对象放入列表
shapes = [r1, c1] #任意类型形状的图形

# 遍历列表并调用area方法
for shape in shapes:
    print(shape.area())
















# import math
#
#
# class Shape:
#     def area(self):
#         pass
#
#
# class Rectangle(Shape):  # 矩形
#     def __init__(self, width, height):
#         self.width = width
#         self.height = height
#
#         def area(self):
#             return self.width * self.height
#
#
# class Circle(Shape):  # 圆
#     def __init__(self, radius):
#         def area(self):
#             return math.pi * radius ** 2
#
#
# r1 = Rectangle(10, 20)
# c1 = Circle(10)
#
# # 将对象放入列表
# shapes = [r1, c1]
#
# # 遍历列表并调用area方法
# for shape in shapes:
#     print(shape.area())
#
# # r1 = Rectangle(10, 20)
# # r1.area()
# # r2 = Circle(10)
# # r2.area()
